package com.jxfgyj.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.jxfgyj.dao.VideoDao;
import com.jxfgyj.domain.DO.jxfgyj_styleEvaluation;
import com.jxfgyj.domain.DO.jxfgyj_video;

public class VideoDaoImpl implements VideoDao {
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public Session getSession() {
		return this.sessionFactory.getCurrentSession();
	}

	/*
	 * 
	 */
	@Override
	public void saveVideo(jxfgyj_video video) {
		Session session = getSession();
		session.save(video);
		session.flush();

	}

	@Override
	public void evaluationVideo(jxfgyj_styleEvaluation styleEvaluation) {
		Session session = getSession();
		session.save(styleEvaluation);
		session.flush();

	}

	@Override
	public int getTeacherStatisticsVoteNum(String video_teacher) {
		Session session = getSession();
		String hql = "select count(distinct styleEvaluation) from jxfgyj_styleEvaluation styleEvaluation,jxfgyj_video video"//
				+ " where video.video_id = styleEvaluation.styleEvaluation_video " //
				+ " and video.video_teacher=:video_teacher";
		//
		Query query = session.createQuery(hql);
		query.setParameter("video_teacher", video_teacher);
		try {
			int count = ((Number) query.uniqueResult()).intValue();
			return count;
		} catch (ClassCastException e) {
			System.err.println(e);
			return 0;
		} finally {
			session.clear();
		}
		//
	}

	@Override
	public double getTeacherStatisticsAverage(String video_teacher) {
		Session session = getSession();
		String hql = "select AVG(styleEvaluation.styleEvaluation_dynamicTalkSpeed"
				+ "+styleEvaluation.styleEvaluation_dynamicLoudness" + "+styleEvaluation.styleEvaluation_dynamicAction"
				+ "+styleEvaluation.styleEvaluation_dynamicHumor"
				+ "+styleEvaluation.styleEvaluation_dynamicFacialExpression) "
				+ "from jxfgyj_styleEvaluation styleEvaluation,jxfgyj_video video"//
				+ " where video.video_id = styleEvaluation.styleEvaluation_video " //
				+ " and video.video_teacher=:video_teacher";
		//
		Query query = session.createQuery(hql);
		query.setParameter("video_teacher", video_teacher);
		try {
			double count = ((Number) query.uniqueResult()).doubleValue();
			return count;
		} catch (ClassCastException e) {
			System.err.println(e);
			return 0;
		} finally {
			session.clear();
		}
	}

	@Override
	public int getTeacherStatisticsHudongNum(String video_teacher) {
		Session session = getSession();
		String hql = "select count(distinct styleEvaluation) from jxfgyj_styleEvaluation styleEvaluation,jxfgyj_video video"//
				+ " where video.video_id = styleEvaluation.styleEvaluation_video " //
				+ " and video.video_teacher=:video_teacher"
				+ " and styleEvaluation.styleEvaluation_staticStyleType='互动型'";
		//
		Query query = session.createQuery(hql);
		query.setParameter("video_teacher", video_teacher);
		try {
			int count = ((Number) query.uniqueResult()).intValue();
			return count;
		} catch (ClassCastException e) {
			System.err.println(e);
			return 0;
		} finally {
			session.clear();
		}
	}

	@Override
	public int getTeacherStatisticsHunheNum(String video_teacher) {
		Session session = getSession();
		String hql = "select count(distinct styleEvaluation) from jxfgyj_styleEvaluation styleEvaluation,jxfgyj_video video"//
				+ " where video.video_id = styleEvaluation.styleEvaluation_video " //
				+ " and video.video_teacher=:video_teacher"
				+ " and styleEvaluation.styleEvaluation_staticStyleType='混合型'";
		//
		Query query = session.createQuery(hql);
		query.setParameter("video_teacher", video_teacher);
		try {
			int count = ((Number) query.uniqueResult()).intValue();
			return count;
		} catch (ClassCastException e) {
			System.err.println(e);
			return 0;
		} finally {
			session.clear();
		}
	}

	@Override
	public int getTeacherStatisticsJiaoshouNum(String video_teacher) {
		Session session = getSession();
		String hql = "select count(distinct styleEvaluation) from jxfgyj_styleEvaluation styleEvaluation,jxfgyj_video video"//
				+ " where video.video_id = styleEvaluation.styleEvaluation_video " //
				+ " and video.video_teacher=:video_teacher"
				+ " and styleEvaluation.styleEvaluation_staticStyleType='教授型'";
		//
		Query query = session.createQuery(hql);
		query.setParameter("video_teacher", video_teacher);
		try {
			int count = ((Number) query.uniqueResult()).intValue();
			return count;
		} catch (ClassCastException e) {
			System.err.println(e);
			return 0;
		} finally {
			session.clear();
		}
	}

	@Override
	public List<jxfgyj_video> listVideo(String search) {
		Session session = getSession();
		String hql = "from jxfgyj_video "//
				+ " where video_name like :search " //
				+ " order by video_name desc";
		//
		Query query = session.createQuery(hql);
		//
		if (null == search || search.equals("")) {
			query.setParameter("search", "%%");
		} else {
			query.setParameter("search", "%" + search + "%");
		}
		//
		List<jxfgyj_video> videoList = query.list();
		session.clear();
		//
		return videoList;
	}

	@Override
	public void deleteVideo_byID(String video_id) {
		Session session = getSession();
		String hql = "delete from jxfgyj_video where video_id=:video_id";
		Query query = session.createQuery(hql);
		query.setParameter("video_id", video_id);
		query.executeUpdate();
	}

	@Override
	public void deleteEvaluation_byVideo(String styleEvaluation_video) {
		Session session = getSession();
		String hql = "delete from jxfgyj_styleEvaluation where styleEvaluation_video=:styleEvaluation_video";
		Query query = session.createQuery(hql);
		query.setParameter("styleEvaluation_video", styleEvaluation_video);
		query.executeUpdate();

	}

	@Override
	public void deleteVideo_byTeacher(String teacher_id) {
		Session session = getSession();
		String hql = "delete from jxfgyj_video where video_teacher=:video_teacher";
		Query query = session.createQuery(hql);
		query.setParameter("video_teacher", teacher_id);
		query.executeUpdate();

	}

	@Override
	public List<jxfgyj_video> listVideo_byTeacher(String teacher_id) {
		Session session = getSession();
		String hql = "from jxfgyj_video "//
				+ " where video_teacher = :video_teacher " //
		;
		//
		Query query = session.createQuery(hql);
		//
		query.setParameter("video_teacher", teacher_id);
		//
		List<jxfgyj_video> videoList = query.list();
		session.clear();
		//
		return videoList;
	}

	@Override
	public void deleteEvaluation(String styleEvaluation_id) {
		Session session = getSession();
		String hql = "delete from jxfgyj_styleEvaluation where styleEvaluation_id=:styleEvaluation_id";
		Query query = session.createQuery(hql);
		query.setParameter("styleEvaluation_id", styleEvaluation_id);
		query.executeUpdate();

	}

	@Override
	public jxfgyj_video getVideo_byID(String video_id) {
		Session session = getSession();
		String hql = "from jxfgyj_video where video_id=:video_id";
		//
		Query query = session.createQuery(hql);
		query.setParameter("video_id", video_id);
		//
		jxfgyj_video video = (jxfgyj_video) query.uniqueResult();
		session.clear();
		return video;
	}

	@Override
	public jxfgyj_styleEvaluation getStyleEvaluation_byVideoAndUser(String video_id, String user_id) {
		Session session = getSession();
		String hql = "from jxfgyj_styleEvaluation "//
				+ " where styleEvaluation_video=:video_id "//
				+ " and styleEvaluation_user=:user_id "//
		;
		//
		Query query = session.createQuery(hql);
		query.setParameter("video_id", video_id);
		query.setParameter("user_id", user_id);
		//

		List<jxfgyj_styleEvaluation> list = query.list();
		session.clear();
		if (list.size() == 0) {
			return null;
		} else {
			return list.get(0);
		}
	}
}
